$(document).ready(function() {
	var ctx = $("#ctx").val(); //获取服务器地址

	/***************************    媒体提现申请列表页    ***************************/

	//通过，或不通过
	$("#btn-pass,#btn-unpass").click(function() {
		var url = "";
		var type = "";
		if ($(this).attr("id") == "btn-pass") {
			url = ctx + "/back/main/finance/finance-management/enable";
			type = "pass";
		} else if ($(this).attr("id") == "btn-unpass") {
			url = ctx + "/back/main/finance/finance-management/disable";
			type = "unpass";
		}
		singlePassOrUnpassOper($(this), url, type);
	});

	//批量通过，或不通过
	$("#all-btn-pass,#all-btn-unpass").click(function() {
		var url = "";
		var type = "";
		if ($(this).attr("id") == "all-btn-pass") {
			url = ctx + "/back/main/finance/finance-management/enable";
			type = "pass";
		} else if ($(this).attr("id") == "all-btn-unpass") {
			url = ctx + "/back/main/finance/finance-management/disable";
			type = "unpass";
		}
		batchPassOrUnpassOper($(this), url, type);
	});

	/***************************    需求方充值    ***************************/

	//校验代理商
	$("#agentListUl_box").mouseleave(function() {
		$('#son_agentListUl').hide(); //ul隐藏
		if ($("#agentId").val() == "" || $("#agentId").val() == -1) {
			$("#main_agentListUl").addClass("mainChange").attr("title", "请选择代理商");
			$("#agentul_selectdiv").find('span.spanVaildate_ul').html("请选择代理商");
		} else {
			$("#main_agentListUl").removeClass("mainChange").attr("title", "");
			$("#agentul_selectdiv").find('span.spanVaildate_ul').html("");
		}
	});

	//初始校验表单
	if ($("#validateForm").html()) {
		$("#validateForm").autovalidate();
	}

	$("#btn-recharge-save").click(function() {
		if ($("#agentId").val() == "" || $("#agentId").val() == -1) {
			$("#main_agentListUl").addClass("mainChange").attr("title", "请选择代理商");
			$("#agentul_selectdiv").find('span.spanVaildate_ul').html("请选择代理商");
			return false;
		}
		if (!$("#validateForm").validate()) {
			return false;
		}
		var falg = validateMoney();
		if (falg == false) {
			return false;
		}
		$("#validateForm").submit();
		return false;
	});

	$("#btn-recharge-cancel").click(function() {
		window.location.href = ctx + "/back/main/finance/finance-management";
	});

	$("#money").blur(function() {
		validateMoney();
	});

	$("#btn-recharge").click(function() {
		var value = $(this).attr("value");
		$.post(ctx + "/back/main/finance/finance-management/backOut", {
			advBalanceId: value
		}, function() {
			refreshData();
		});
	});
});

function validateMoney() {
	var reg = /^\d{1,8}\.?\d{0,2}$/;
	var $this = $("#money");
	if ($this.val().match(reg) != null) {
		if (($this.val().indexOf('.') == -1 && $this.val().length > 8) || $this.val() <= 0) {
			$this.css("box-shadow", "0 0 6px #ff0000").attr('title', '分配金额必须大于0且整数位最多8位');
			$this.parent("div").find("span.spanVaildate").html("分配金额必须大于0且整数位最多8位");
			return false;
		} else {
			$this.css("box-shadow", "none").attr('title', '');
			$this.parent("div").find("span.spanVaildate").html("");
		}
	} else {
		$this.css("box-shadow", "0 0 6px #ff0000").attr('title', '分配金额必须大于0整数位最多8位且最多有2位小数');
		$this.parent("div").find("span.spanVaildate").html("分配金额必须大于0整数位最多8位且最多有2位小数");
		return false;
	}
}